#!/usr/bin/env node

const q = document.querySelector,
      $ = q.bind(document);

const $width = $('[name="width"]'),
      $height = $('[name="height"]'),
      $calculate = $('[type="button"]'),
      $perimeter = $('[name="perimeter"]'),
      $area = $('[name="area"]');

const baseURL = 'http://192.168.189.131:3000/api?';
$calculate.onclick = () => {
    const rect = {
        width: Number($width.value),
        height: Number($height.value),
    };   

    let url = new URLSearchParams(rect).toString();
    // console.log(url);    
    url = baseURL + url;
    
    fetch(url)
    .then(res => res.json())
    .then(rect => {
        $perimeter.value = rect.perimeter;
        $area.value = rect.area;
    })
    .catch(error => {
        console.error('Error:', error);
    });
}



